<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"  xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"  xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<meta charset="utf-8">
<head th:include="centre/manage/include :: header">
</head>
<link rel="stylesheet" type="text/css" href="/css/plugins/layui/layui.css"/>
<!-- <link rel="stylesheet" type="text/css" href="/public/ace/css/ace.min.css" class="ace-main-stylesheet" id="main-ace-style" /> -->

<style>
input[type=email], input[type=url], input[type=search], input[type=tel],
	input[type=color], input[type=text], input[type=password], input[type=datetime],
	input[type=datetime-local], input[type=date], input[type=month], input[type=time],
	input[type=week], input[type=number], textarea {
	border-radius: 0 !important;
	color: #858585;
	background-color: #FFF;
	border: 1px solid #D5D5D5;
	padding: 5px 4px 6px;
	font-size: 14px;
	font-family: inherit;
	-webkit-box-shadow: none !important;
	box-shadow: none !important;
	-webkit-transition-duration: .1s;
	transition-duration: .1s;
}

select {
    padding: 3px 4px;
    height: 30px;
}

.table{
	margin-bottom: 0px;
	table-layout: fixed;
}
</style>
<body class="gray-bg">
	<div class="wrapper wrapper-content">
		<div class="row">
			<div class="col-sm-12">
			<div class="panel panel-info">
				<div class="panel-heading">商品库商品销售统计列表</div>
			    <div class="panel-body">
			        <div class="form-horizontal">
		             <div class="form-group" style="padding-left:60px;">
		              <div class="col-xs-12 col-sm-3 hidden-xs btn-sespan" >
						<div class="input-group">
							<span class="input-group-addon" style="height:30px;">
								付款时间
							</span>
							<input type="text" name="payStartTime" id="payStartTime"
								   value="" placeholder="点击选择开始时间"
								   readonly class="datetimepicker form-control"
								   style="height:30px;"/>
							<span>
								<i class="fa fa-exchange"></i>
							</span>
							<input type="text"  name="payEndTime" id="payEndTime"
								   value="" placeholder="点击选择结束时间"
								   readonly class="datetimepicker form-control"
								   style="height:30px;"/>

						</div>
					 </div>
					 <div class="col-xs-12 col-sm-2 hidden-xs btn-sespan" >
	                   <select class="form-control tpl-category-parent" id="pcate"
							name="pcate" onchange="renderCategoryThird(this)">
							<option value="">顶级分类</option>
							<option th:each="item: ${category}" th:text="${item.name}"
								th:value="${item.id}"></option>
								
						</select>
						<select class="form-control tpl-category-child" id="ccate"
							name="ccate">
							<option value="">下级分类</option>
						</select>
		             </div>
		             <div class="col-xs-12 col-sm-2 hidden-xs btn-sespan" data-toggle="distpicker" id="dist_target">
	                    <select class="form-control tpl-category-parent" id="province" name="province" data-province="----选择省----">
						</select>
						<select class="form-control tpl-category-child"  id="city" name="city" data-city="----选择市----">
						</select>
		             </div>
					 <div class="col-xs-12 col-sm-1 hidden-xs btn-sespan" >
	                   <div class="input-group">
	                   		<select class="form-control" id="payState"
								name="payState">
								<option value="2">支付完成</option>
								<option value="0">未支付</option>
								<option value="1">支付中</option>
								<option value="3">支付失败</option>
								<option value="4">代付申请</option>
								<option value="5">待付中</option>
								<option value="6">代付成功</option>
								<option value="7">代付失败</option>
								<option value="8">退款申请</option>
								<option value="9">退款中</option>
								<option value="10">退款成功</option>
								<option value="11">退款失败</option>
							</select>
						</div>
		             </div>
		              <div class="col-xs-12 col-sm-1 hidden-xs btn-sespan" >
	                   <div class="input-group">
	                   		<select class="form-control" id="status" name="status">
								<option value="">-订单状态-</option>
								<option value="1">待发货</option>
								<option value="2">待收货</option>
								<option value="3">已收货</option>
								<option value="4">待退款</option>
								<option value="5">售后中</option>
								<option value="6">已完成</option>
								<option value="-1">已取消</option>
								<option value="0">未支付</option>
							</select>
						</div>
		             </div>
		             <div class="col-xs-12 col-sm-2 hidden-xs btn-sespan" >
	                   <select class="form-control tpl-category-parent" id="supplyName">
							<option value="0">按商品名称</option>
							<option value="1">按供应商</option>
						</select>
						<input name="title" id="title" type="text" class="form-control"
								value="" placeholder="输入搜索内容">
		             </div>
		             <input type="hidden" name="centreId" th:value="${centreId}" th:if="${flag} == '1'">
					 <div class="col-xs-12 col-sm-2 hidden-xs btn-sespan" th:if="${flag} == '0'">
						<select data-placeholder="商户" class="form-control chosen-select" 
						id="centreId" name="centreId" tabindex="2"  onchange="renderCategoryThird2(this)">
							<option value="">选择商户</option>
							<option th:each="item: ${centreList}" th:text="${item.name}" th:value="${item.centreId}"></option>
						</select>
					 </div>
					 
					 <div class="col-xs-12 col-sm-2 hidden-xs btn-sespan" >
							<select  id="liansuoid"
								class="form-control chosen-select" tabindex="2">
								<option value="" selected="selected">选择门店</option>
								<option th:each="item: ${teams}" th:text="${item.tXiaoqu+ +item.id}"
                                        th:value="${item.id}"></option>
							</select>
						</div>
						
							<div class="col-xs-12 col-sm-2 hidden-xs btn-sespan">
							<select id="isSource"
								class="form-control chosen-select" tabindex="2">
								<option value >选择订单商品来源</option>
								  <option value="0" >非门店</option>
								  <option value="1" >门店</option>
							</select>
						</div>
		            </div>
		            <!-- <div class="form-group">
		                <label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label">排序方式</label>
		                <div class="col-sm-8 col-lg-9 col-xs-12">
		                    <label class='radio-inline'>
		                        <input type='radio' name='orderby' value='0' /> 按销售额
		                    </label>
		                    
		                    <label class='radio-inline'>
		                        <input type='radio' name='orderby' value='1'  checked/> 按销售量
		                    </label>
		                    <label class='radio-inline'>
		                        <input type='radio' name='orderby' value='2'  /> 按付款时间
		                    </label>
		                </div>
		            </div> -->
		            
		            <div class="form-group">
		                <label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label"></label>
		                <div class="col-sm-8 col-lg-9 col-xs-12">
		                    <button class="btn btn-default" onclick="reLoad()">
		                    	<i class="fa fa-search"></i> 搜索
		                    </button>
		                    <button type="button" class="btn btn-default" style='margin-left:10px;' onclick="exportExcel()">
								<span class="fa fa-download"></span>
								导出Excel
							</button>
		                </div>
		            </div>
			        </div>
			    </div>
			</div>

			<div class="panel panel-default">
				<div class="panel-body" style="padding: 0;">
					<table id="productbaseSaleListTable" data-mobile-responsive="true">
					</table>
					<table>
						<tr>
							<td height="50" colspan="7" align="right">总合计:  </td>
							<td align="center">
								<label class="btn btn-minier btn-info" id="totalNum"> 0</label>
							</td>
							<td height="50" colspan="12" align="center"><label class="btn btn-minier btn-success" id="totalAmount"> 0 </label></td>
						</tr>
					</table>
				</div>
			</div>

			<!--<div class="panel panel-default">
    			<div class="panel-body" style="padding: 0;">
    				<table id="productbaseSaleListTable" data-mobile-responsive="true">
					</table>
    			</div>
			</div>-->
				<div shiro:hasPermission="report:manage:productSaleDetail">
					<script type="text/javascript">
                        s_edit_h = '';
					</script>
				</div>
		</div>
			
		</div>
		</div>
	<div th:include="centre/manage/include::footer"></div>
	<script type="text/javascript" src="/public/ace/js/ace-extra.js"></script>
	<script type="text/javascript" src="/js/plugins/layui/layui.js"></script>
	<script type="text/javascript">
		function closeDow() {
			var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
			parent.layer.close(index);
		}
        layui.use('laydate', function(){
            var laydate = layui.laydate;
            laydate.render({
                elem: '#payStartTime' , type: 'datetime'
            });

            laydate.render({
                elem: '#payEndTime' , type: 'datetime'
            });
        })


		var prefix = "/report/manage";
		$(document).ready(function () {
			$("#province").val("");
			$("#city").val("");
            var date = new Date();
            var year = date.getFullYear();
            var mon=date.getMonth()+1 < 10 ? "0"+(date.getMonth()+1+""):date.getMonth()+1;
            var da=date.getDate() < 10 ? "0"+ date.getDate():date.getDate();
            date.setDate(date.getDate()-1);
            var endyear = date.getFullYear();
            var endmon=date.getMonth()+1 < 10 ? "0"+(date.getMonth()+1+""):date.getMonth()+1;
            var endda=date.getDate() < 10 ? "0"+ date.getDate():date.getDate();
            $("#payEndTime").val(year+"-"+mon+"-"+da+" 20:05:00");
            $("#payStartTime").val(endyear+"-"+endmon+"-"+endda+" 20:30:00");
		    load();
		});
		function load() {
			$('#productbaseSaleListTable').bootstrapTable({
				method : 'get', // 服务器数据的请求方式 get or post
				url : prefix + "/productbaseSale", // 服务器数据的加载地址
				showRefresh : true,
				// showToggle : true,
				iconSize : 'outline',
				// toolbar: '#exampleToolbar',
				striped : true, // 设置为true会有隔行变色效果
				dataType : "json", // 服务器返回的数据类型
				pagination : true, // 设置为true会在底部显示分页条
				pageList : [ 10, 20, 50 ],
				// queryParamsType : "limit",
				// //设置为limit则会发送符合RESTFull格式的参数
				singleSelect : false, // 设置为true将禁止多选
				// contentType : "application/x-www-form-urlencoded",
				// //发送到服务器的数据编码类型
				pageSize : 10, // 如果设置了分页，每页数据条数
				pageNumber : 1, // 如果设置了分布，首页页码
				// search : true, // 是否显示搜索框
				showColumns : true, // 是否显示内容下拉框（选择显示的列）
				showFooter : true,
				sidePagination : "server", // 设置在哪里进行分页，可选值为"client" 或者
				// "server"
				queryParams : function(params) {
                    if($.trim($("#supplyName").val())==0){
                        var title = $.trim($("#title").val());
                    }else{
                        var supplyName = $.trim($("#title").val());
                    }
					return {
						limit : params.limit,
						offset : params.offset ,
						payStartTime: $.trim($("#payStartTime").val()),
						payEndTime: $.trim($("#payEndTime").val()),
						pcate: $.trim($("#pcate").val()),
						ccate: $.trim($("#ccate").val()),
						province: $.trim($("#province").val()),
						city: $.trim($("#city").val()),
						payState:$.trim($("#payState").val()),
						title:title,
                        supplyName:supplyName,
                        centreId:$.trim($("#centreId").val()),
                        status:$.trim($("#status").val()),
                        liansuoid:$.trim($("#liansuoid").val()),
                        isSource:$.trim($("#isSource").val())
					};
				},
				columns: [
                {
                    title: '商品名称',
                    field: 'productName',
                    width: 200
                },
                {
                    title: '一级分类',
                    field: 'pcateName'
                },{
                    title: '二级分类',
                    field: 'ccateName'
                },{
                    title: '供应商',
                    field: 'supplyName'
                },
                {
                    title: '买手',
                    field: 'buyrealnm'
                },
                 {
                    title: '商品型号',
                    field: 'specTitle'
                }, 
                {
                    title: '商品Id',
                    field: 'productId'
                },
                {
                    title: '规格名称',
                    field: 'optionname'
                },
                {
                    title: '销售价',
                    field: 'marketprice'
                },{
                    title: '门店佣金',
                    field: 'headcommission'
                },{
                    title: '站点成本价',
                    field: 'costprice',
				    footerFormatter : function(value) {
                    	return "当前页合计：";
                    }
                },{
                    title: '平台成本价',
                    field: 'bcostprice'
                },
                /* {
                    title: '上架时间',
                    field: 'upStartTime'
                }, */
                {
                    title: '销售数量',
                    field: 'sumTotal',
				    footerFormatter : function(value) {
				    	var count = 0;
				    	for (var i=0;i<value.length;i++) {
				    		count += value[i].sumTotal;
				    	}
                    	return "销售数量：" + count;
                    }
                },{
                    title: '销售金额',
                    field: 'sumPrice',
				    footerFormatter : function(value) {
				    	var count = 0;
				    	for (var i=0;i<value.length;i++){
				    		count += value[i].sumPrice*100;
				    	}
                    	return "销售金额:￥" + count/100;
                    }
                },
                    {
                        title: '操作',
                        //field: 'productId',
                        align: 'center',
                        formatter: function (value, row, index) {
                            var centreId = $.trim($("#centreId").val());
                            if (!centreId){
								var e = '<a class="btn btn-primary btn-sm ' + s_edit_h + '" href="#" mce_href="#" title="编辑" onclick="edit('
									+ row.productId + ","+ row.skuId + ')"><i class="fa fa-edit"></i>销量详情</a> ';
								return e;
                            }
                        }
                    }],
                onPostBody: function(){
                    countNum();
                }
			});
		}
		function reLoad() {
			$('#productbaseSaleListTable').bootstrapTable('refresh');
		}
		
		function renderCategoryThird(obj) {
			var index = obj.options[obj.selectedIndex].value;
			var html = "<option>请选择下级分类</option>";
			if (index == 0) {
				$("#ccate").html(html);
			}else {
				$.ajax({
					url: "/category/manage/getChildByPar",
					data:{"id":index},
					dataType: "json",
					success: function(data){
						for(var i=0; i< data.length; i++){
							html += '<option value="'+data[i].id+'">'+data[i].name+'</option>';
						}
						$("#ccate").html(html);
					}
				})
			}
		}

        function exportExcel() {
            var title='';
            var supplyName='';
            var payStartTime = $.trim($("#payStartTime").val());
            var payEndTime = $.trim($("#payEndTime").val());
            var province = $.trim($("#province").val());
            var city = $.trim($("#city").val());
            if($.trim($("#supplyName").val())==0){
                title = $.trim($("#title").val());
            }else{
                supplyName = $.trim($("#title").val());
            }
            var payState = $.trim($("#payState").val());
            var centreId = $.trim($("#centreId").val());
            var pcate = $.trim($("#pcate").val());
            var ccate = $.trim($("#ccate").val());
            var liansuoid = $.trim($("#liansuoid").val());
            var isSource = $.trim($("#isSource").val());
            window.location.href = prefix + "/exportProductbaseSale?payStartTime=" + payStartTime +
                "&payEndTime=" + payEndTime +"&province=" + province +"&city=" + city +
                "&payState=" + payState +"&pcate=" + pcate +"&centreId=" + centreId +
                "&title=" + title + "&ccate=" + ccate+ "&supplyName=" + supplyName+"&liansuoid="+liansuoid
                +"&isSource="+isSource;
        }
        function edit(productId,skuId) {
            if (!skuId)skuId=0;
            var title='';
            var supplyName='';
            var payStartTime = $.trim($("#payStartTime").val());
            var payEndTime = $.trim($("#payEndTime").val());
            var province = $.trim($("#province").val());
            var city = $.trim($("#city").val());
            if($.trim($("#supplyName").val())==0){
                 title = $.trim($("#title").val());
            }else{
                 supplyName = $.trim($("#title").val());
			}
            var payState = $.trim($("#payState").val());
            var centreId = $.trim($("#centreId").val());
            var pcate = $.trim($("#pcate").val());
            var ccate = $.trim($("#ccate").val());
            layer.open({
                id:'warehouse_edit',
                type : 2,
                title : '销量详情',
                maxmin : true,
                shadeClose : false,
                area : [ '30%', '40%' ],
                //content : prefix + '/saleDetail/' + productId + '/' + skuId // iframe的url
                content : prefix + "/saleDetail?payStartTime=" + payStartTime +
                "&payEndTime=" + payEndTime +"&province=" + province +"&city=" + city +
                "&payState=" + payState +"&pcate=" + pcate +"&centreId=" + centreId +
                "&title=" + title + "&ccate=" + ccate+ "&productId=" + productId+ "&skuId=" + skuId+ "&supplyName=" + supplyName
            });
        }

        function countNum() {
            var title='';
            var supplyName='';
            var payStartTime=$.trim($("#payStartTime").val());
            var payEndTime=$.trim($("#payEndTime").val());
            var pcate=$.trim($("#pcate").val());
            var ccate=$.trim($("#ccate").val());
            var province=$.trim($("#province").val());
            var city=$.trim($("#city").val());
            var payState=$.trim($("#payState").val());
            var centreId=$.trim($("#centreId").val());
            var status=$.trim($("#status").val());
            if($.trim($("#supplyName").val())==0){
                title = $.trim($("#title").val());
            }else{
                supplyName = $.trim($("#title").val());
            }
            var liansuoid = $.trim($("#liansuoid").val());
            var isSource = $.trim($("#isSource").val());
			$.ajax({
				url: prefix+"/productTotalbaseSale",
				data:{"payStartTime":payStartTime,"payEndTime":payEndTime,"title":title,"supplyName":supplyName,
					"pcate":pcate,"ccate":ccate,"province":province,"city":city,"payState":payState,"centreId":centreId,"status":status,
					"liansuoid":liansuoid,"isSource":isSource},
				dataType: "json",
				success: function(data){
					 
					if(data){
						 
					
						if(data.sumTotalNum){
							$("#totalNum").html(data.sumTotalNum+" 单");
						}else{
							$("#totalNum").html("0 单");
						}
						if(data.sumTotalPrice){
							$("#totalAmount").html(data.sumTotalPrice+" 元");
						}else{
							$("#totalAmount").html("0 元");
						}

					} 
				}
			})
        }
        
        function renderCategoryThird2(obj) {
			var index = obj.options[obj.selectedIndex].value;
			var html = "<option value="">请选择门店</option>";
			if (index == 0) {
				$("#liansuoid").html(html);
			}else {
				$.ajax({
					url: "/commanderList/manage/listCommanders",
					data:{"centreId":index},
					dataType: "json",
					success: function(data){
						for(var i=0; i< data.length; i++){
							html += '<option value="'+data[i].id+'">'+data[i].tXiaoqu+ data[i].id+'</option>';
						}
						$("#liansuoid").html(html);
					}
				})
			}
		}
	</script>
	
</body>
</html>
